﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class RichengDAO : BaseService
    {
        /// <summary>
        /// 查询过期日程
        /// </summary>
        /// <param name="state"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Richeng> GetListPagingHistory(int state,int userid, int page, int pagesize)
        {
            string hql = "from Richeng where BeginDate <getdate()  and IsDel=false and IsPub = false and UserId = "+userid+"  order by BeginDate desc";
            return FindList<Richeng>(hql, null, (page - 1) * pagesize, pagesize);
        }
        /// <summary>
        /// 查询将到日程
        /// </summary>
        /// <param name="state"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Richeng> GetListPagingFuture(int state,int userid, int page, int pagesize)
        {
            string hql = "from Richeng where BeginDate >=getdate() and IsDel=false and IsPub = false and UserId = "+userid+" order by BeginDate desc";
            return FindList<Richeng>(hql, null, (page - 1) * pagesize, pagesize);
        }
        /// <summary>
        /// 查询过期日程个数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetListPagingHistory(int state,int userid)
        {
            string hql = "select count(*) from Richeng where BeginDate<getdate()  and IsDel=false and IsPub = false and UserId =  " + userid;
            if (state == -99)
            {
                //hql += " and Id = " + id;
            }
            else
            {
                hql += " and State =" + state;
            }
            return Convert.ToInt32(GetValue(hql));
        }
        /// <summary>
        /// 查询将到日程个数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetListPagingFuture(int state,int userid)
        {
            string hql = "select count(*) from Richeng where BeginDate >=getdate() and IsDel=false and IsPub = false and UserId ="+userid;
            if (state == -99)
            {
                //hql += " and Id = " + id;
            }
            else
            {
                hql += " and State =" + state;
            }
            return Convert.ToInt32(GetValue(hql));
        }
        /// <summary>
        /// 查询待完成日程
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Richeng> GetListPaging(int userid, bool isPub, bool isDel, int page, int pagesize)
        {
            string hql = "from Richeng where 1=1 and UserId=" + userid + " and IsPub=" + isPub + " and IsDel = " + isDel + " order by AddDate desc";
            try
            {
                return FindList<Richeng>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch (Exception)
            {
                return null;
               
            }
        }
        /// <summary>
        /// 查询待完成日程数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetListPaging(int userid, bool isPub, bool isDel)
        {
            string hql = "select count(*) from Richeng where 1=1 and UserId=" + userid + " and IsPub=" + isPub + " and IsDel = " + isDel ;
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {
                
                return 0;
            }
        }

        /// <summary>
        /// select all 
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public IList<Richeng> GetListPaging(int userid, bool isDel, int page, int pagesize)
        {
            string hql = "from Richeng where 1=1 and UserId=" + userid + " and  IsDel = " + isDel + " order by AddDate desc";
            try
            {
                return FindList<Richeng>(hql, null, (page - 1) * pagesize, pagesize);
            }
            catch (Exception)
            {
                return null;

            }
        }


        /// <summary>
        /// 查询删除日程数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="isPub"></param>
        /// <param name="isDel"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public int GetListPaging(int userid, bool isDel)
        {
            string hql = "select count(*) from Richeng where 1=1 and UserId=" + userid + " and  IsDel = " + isDel;
            try
            {
                return Convert.ToInt32(GetValue(hql));
            }
            catch (Exception)
            {

                return 0;
            }
        }


         /// <summary>
        /// 查询全部日程个数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public IList<Richeng> GetListPaging(int userid,int page,int pagesize)
        {
            string hql = "from Richeng where IsDel=false and IsPub = false and UserId =  " + userid;
            return   FindList<Richeng>(hql, null, (page - 1) * pagesize, pagesize);
        }
        /// <summary>
        /// 查询全部日程个数
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        public int GetListPaging(int userid)
        {
            string hql = "select count(*) from Richeng where IsDel=false and IsPub = false and UserId =  " + userid;
            return Convert.ToInt32(GetValue(hql));
        }


        /// <summary>
        /// 批量修改暂存
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchRichengUpdate(string ids)
        {
            string hql = "update Richeng set IsPub = false where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量修改删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchRichengDelete(string ids)
        {
            string hql = "update Richeng set IsDel = true where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量修改暂存
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchRRichengUpdate(string ids)
        {
            string hql = "update Richeng set IsDel = false where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

        /// <summary>
        /// 批量修改删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>

        public Boolean BatchRRichengDelete(string ids)
        {
            string hql = "delete Richeng  where Id in (" + ids + ")";
            try
            {
                DBHelper.TransDeleteByHQL(hql);
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
    }
}
